//
//  AxisCell.m
//  Organic
//
//  Created by sunx on 15/3/22.
//  Copyright (c) 2015年 Mike Amaral. All rights reserved.
//

#import "AxisCell.h"
#import "ImageLoader.h"
@interface AxisCell()
{
    int refreshTimes;
}
@property (nonatomic,strong) NSString *url;
@property (nonatomic,strong) NSTimer* timer;
@end
@implementation AxisCell

@synthesize imageView;

-(instancetype)initWithImage:(NSString *)imageURL andBody:(NSString *)body andDate:(NSString *)date{
    self = [super init];
    refreshTimes = 0;
    //获取屏幕宽度
    CGRect r = [ UIScreen mainScreen ].applicationFrame;
    float realWidth = r.size.width;
    float dateHeight = 40;
    
    self.imageView = [[UIImageView alloc] init];
    UIImage *image = [ImageLoader loardImageToDocuments:imageURL];
    self.url = imageURL;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(refreshSelf) userInfo:nil repeats:YES];
    if(image == nil) {
        image = [UIImage imageNamed:@"article"];
        [self.timer fire];
    }
    
    self.imageView.image = image;
    
    float imageHeight = (image.size.height/image.size.width) * realWidth *0.9;
    self.imageView.frame = CGRectMake(r.size.width*0.05, dateHeight + 5, realWidth*0.9, imageHeight);
    
    self.imageView.image = image;
   
    //时间label
    self.date = [[UILabel alloc] initWithFrame:CGRectMake(realWidth*0.05, 10, realWidth, dateHeight)];
    self.date.text = date;
    self.date.font = [UIFont fontWithName:@"Helvetica" size:30];
    self.date.textColor = [UIColor whiteColor];
    
    //内容body
    self.body = [[UITextView alloc] initWithFrame:CGRectMake(realWidth*0.1, imageHeight +dateHeight, realWidth *0.8, 100)];
   // NSLog(@"%@",body);
    self.body.font = [UIFont fontWithName:@"Helvetica" size:15];
    self.body.text = body;

    //计算text view高度
    float bodyHeight;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        
        CGRect textFrame=[[self.body layoutManager]usedRectForTextContainer:[self.body textContainer]];
        bodyHeight = textFrame.size.height;
        
    }else {
        bodyHeight = self.body.contentSize.height;
    }
    bodyHeight += 20;

    //设置高度
    self.body.frame = CGRectMake(realWidth*0.05, 0, realWidth *0.8, bodyHeight);
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(realWidth*0.05, imageHeight +dateHeight +15, realWidth * 0.9, bodyHeight)];
    [view setBackgroundColor:[UIColor whiteColor]];
    [view addSubview:self.body];
    [self.contentView addSubview:view];
    self.body.backgroundColor = [UIColor whiteColor];
    self.body.textColor = [UIColor grayColor];
    self.body.editable = NO;

    
    //cell设置
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self.contentView addSubview:self.date];
    [self.contentView addSubview:self.imageView];
    
    //更新cell高度
    self.height = dateHeight + imageHeight + bodyHeight + 15;
    return self;
}
- (void)refreshSelf {
    NSLog(@"");
    refreshTimes ++;
    UIImage *image = [ImageLoader loardImageToDocuments:self.url];
    if(image != nil) {
        [self.timer invalidate];
        self.imageView.image = image;
    }
    if(refreshTimes > 15) {
        [self.timer invalidate];
    }
}

@end
